package com.wl.mvi.main.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.flyco.tablayout.CommonTabLayout
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.wl.lib.mvi.BaseBindingFragment
import com.wl.lib.widget.RecyclerAdapter
import com.wl.mvi.databinding.FragmentBlogBinding
import com.wl.mvi.main.tab.TabOne
import com.wl.mvi.main.tab.TabOneItem
import com.wl.mvi.main.tab.TabOneView
import com.wl.mvi.main.tab.TabTwo
import com.wl.mvi.main.tab.TabTwoItem
import com.wl.mvi.main.tab.TabTwoView


// Created by wangliang on 2024/4/25.
class BlogFragment : BaseBindingFragment<FragmentBlogBinding>() {
    companion object {
        private const val TAG = "BlogFragment"
        private val TABS = listOf("Tab11", "Tab2");
    }

    private val adapter: RecyclerAdapter by lazy {
        RecyclerAdapter().apply {
            val lp = {
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            }

            registerViewType<TabOneView, TabOne>(lp) { itemView, item, _ ->
                itemView.render(item)
            }
            registerViewType<TabTwoView, TabTwo>(lp) { itemView, item, _ ->
                itemView.render(item)
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val tabEntities = arrayListOf<CustomTabEntity>()
        TABS.forEach { tabEntities.add(TabEntity(it)) }
        binding.tabLayout.setTabData(tabEntities)
        binding.viewPager.adapter = adapter
        binding.tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                binding.viewPager.setCurrentItem(position, true)
            }

            override fun onTabReselect(position: Int) {
            }
        })


        adapter.data.add(TabOne(arrayListOf(TabOneItem("微乐斗地主"), TabOneItem("微乐棋牌"), TabOneItem("微乐棋牌"))))
        adapter.data.add(
            TabTwo(
                arrayListOf(
                    TabTwoItem("微乐斗地主"),
                    TabTwoItem("微乐棋牌"),
                    TabTwoItem("微乐棋牌"),
                    TabTwoItem("微乐棋牌")
                )
            )
        )

        binding.viewPager.registerOnPageChangeCallback(TabLayoutOnPageChangeListener(binding.tabLayout))
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("WLTest", "BlogFragment onCreateView")
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onStart() {
        Log.d("WLTest", "BlogFragment onStart")
        super.onStart()
    }

    override fun onResume() {
        Log.d("WLTest", "BlogFragment onResume")
        super.onResume()
    }

    override fun onPause() {
        Log.d("WLTest", "BlogFragment onPause")
        super.onPause()
    }

    override fun onStop() {
        Log.d("WLTest", "BlogFragment onStop")
        super.onStop()
    }

    override fun onDestroyView() {
        Log.d("WLTest", "BlogFragment onDestroyView")
        super.onDestroyView()
    }
}


private class TabEntity(val title: String) : CustomTabEntity {
    override fun getTabTitle(): String {
        return title
    }

    override fun getTabSelectedIcon(): Int = 0

    override fun getTabUnselectedIcon(): Int = 0
}

private class TabLayoutOnPageChangeListener(val tabLayout: CommonTabLayout) : OnPageChangeCallback() {
    private var previousScrollState = 0
    private var scrollState = 0
    private var hasDragging = false

    override fun onPageScrollStateChanged(state: Int) {
        previousScrollState = scrollState
        scrollState = state
        if (previousScrollState == ViewPager2.SCROLL_STATE_IDLE) {
            hasDragging = false
        }
        hasDragging =
            (previousScrollState == ViewPager2.SCROLL_STATE_DRAGGING || scrollState == ViewPager2.SCROLL_STATE_DRAGGING)
    }

    override fun onPageSelected(position: Int) {
        if (tabLayout.currentTab != position && position < tabLayout.tabCount) {
            // Select the tab, only updating the indicator if we're not being dragged/settled
            // (since onPageScrolled will handle that).
            if (hasDragging)
                tabLayout.currentTab = position
        }
    }
}